package servlet;

import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * @author MissChen
 * @date 2024年04月23日 10:27
 */
@WebServlet("/showModifyEmp")
public class ShowModifyEmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "root");
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .append(" where id=? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.parseInt(id));
            rs = ps.executeQuery();
            if (rs.next()) {

                out.println("<!DOCTYPE html>");
                out.println("<html lang='en'>");
                out.println("    <head>");
                out.println("         <meta charset='UTF-8'>");
                out.println("        <title>修改员工信息</title>");
                out.println("    </head>");
                out.println("    <body>");
                out.println("        <h1>修改员工信息</h1>");
                out.println("        <a href='findAll'>返回员工列表</a>");
                out.println("        <hr>");
                out.println("        <form action='modifyEmp' method='post'>");
                out.println("       编号:<input type='text' name='id' readonly  value='"+rs.getInt("id")+"'><br>");
                out.println("       姓名:<input type='text' name='name' value='"+rs.getString("name")+"'><br>");
                out.println("            工资:<input type='text' name='salary' value="+rs.getDouble("salary")+"><br>");
                out.println("            性别:");
                out.println("            <input type='radio' name='gender' value='man'"+(rs.getString("gender").equals("man")?"checked":"")+">男");
                out.println("            <input type='radio' name='gender' value='woman'"+(rs.getString("gender").equals("woman")?"checked":"")+">女");
                out.println("            <br>");
                out.println("            学历:");
                out.println("            <select name='edu'>");
                out.println("               <option value='-1'>请选择</option>");
                out.println("               <option value='1'"+(rs.getString("edu").equals("1")?"selected":"")+">高中</option>");
                out.println("               <option value='2'"+(rs.getString("edu").equals("2")?"selected":"")+">大专</option>");
                out.println("               <option value='3'"+(rs.getString("edu").equals("3")?"selected":"")+">本科</option>");
                out.println("               <option value='4'"+(rs.getString("edu").equals("4")?"selected":"")+">研究生</option>");
                out.println("           </select>");
                out.println("           <br>");
                out.println("           兴趣:");
                out.println("           <input type='checkbox' name='hobbies' value='eat'"+ StringUtil.checkHobby(rs.getString("hobbies"),"eat")+">吃饭");

                out.println("           <input type='checkbox' name='hobbies' value='sleep'"+StringUtil.checkHobby(rs.getString("hobbies"),"sleep")+">睡觉");
                out.println("           <input type='checkbox' name='hobbies' value='beat'"+StringUtil.checkHobby(rs.getString("hobbies"),"beat")+">打豆豆");
                out.println("           <input type='submit'  value='提交'>");
                        out.println("        </form>");
                out.println("    </body>");
                out.println("</html>");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();

        }
    }
}
